/**
 * Created by Administrator on 2016/3/2.
 */
//qq大礼包
gpNamespace.QqlbLayer = cc.Layer.extend({
	lbDailyPic: null,
	rewardIndex: null,//当前点击的连续礼包index
	
    ctor: function () {
        this._super();

        this.init();
    },
    init: function () {
    	cc.spriteFrameCache.addSpriteFrames(gpNamespace.pdkqqqiandaoRes.Pdkqqqiandao_plist);
    	cc.spriteFrameCache.addSpriteFrames(gpNamespace.sctcRes.Sctc_plist);
        var size = cc.winSize;

        //背景图层
        var background = new cc.Sprite(gpNamespace.qqlb.Qqlb_Bg);
        background.attr({
            x: 63+background.width/2 ,
            y: 87+background.height/2 ,
            anchorX: 0.5,
            anchorY: 0.5,
        });
        this.addChild(background);

        //每日礼包
        var lbDaily= new cc.Sprite(gpNamespace.qqlb.Qqlb_Daily);
        lbDaily.attr({
            x: 107+lbDaily.width/2,
            y: 418+lbDaily.height/2,
            anchorX: 0.5,
            anchorY: 0.5,
        });
        this.addChild(lbDaily);
        //7日礼包
        var lb7days= new cc.Sprite(gpNamespace.qqlb.Qqlb_7days);
        lb7days.attr({
            x: 310+lb7days.width/2,
            y: lbDaily.getPositionY(),
            anchorX: 0.5,
            anchorY: 0.5,
        });
        this.addChild(lb7days);
        //14日礼包
        var lb14days= new cc.Sprite(gpNamespace.qqlb.Qqlb_14days);
        lb14days.attr({
            x: 465+lbDaily.width/2,
            y: lbDaily.getPositionY(),
        });
        this.addChild(lb14days);
        //21日礼包
        var lb21days= new cc.Sprite(gpNamespace.qqlb.Qqlb_21days);
        lb21days.attr({
            x: 660+lb21days.width/2,
            y: lbDaily.getPositionY(),
        });
        this.addChild(lb21days);
        //28日礼包
        var lb28days= new cc.Sprite(gpNamespace.qqlb.Qqlb_28days);
        lb28days.attr({
            x: 838+lb28days.width/2,
            y: lbDaily.getPositionY(),
        });
        this.addChild(lb28days);
        //月礼包
        var lbMonth= new cc.Sprite(gpNamespace.qqlb.Qqlb_Month);
        lbMonth.attr({
            x: 1020+lbMonth.width/2,
            y: lbDaily.getPositionY(),
        });
        this.addChild(lbMonth);

        var lbDailyPicOn = new cc.MenuItemImage(gpNamespace.qqlb.Qqlb_DailyPic);
        var lbDailyPicOff = new cc.MenuItemImage(gpNamespace.qqlb.Qqlb_DailyPic);
        lbDailyPicOff.opacity = 128;
        var lbDailyPic = new cc.MenuItemToggle(lbDailyPicOff, lbDailyPicOn, this.lbCallback, this);
        this.lbDailyPic=lbDailyPic;
        lbDailyPic.setEnabled(false);
        lbDailyPic.setUserData(0);

        var lb7daysPicOn = new cc.MenuItemImage(gpNamespace.qqlb.Qqlb_7daysPic);
        var lb7daysPicOff = new cc.MenuItemImage(gpNamespace.qqlb.Qqlb_7daysPic);
        lb7daysPicOff.opacity = 128;
        var lb7daysPic = new cc.MenuItemToggle(lb7daysPicOff, lb7daysPicOn, this.lbCallback, this);
        this.lb7daysPic=lb7daysPic;
        lb7daysPic.setEnabled(false);
//      lb7daysPic.setUserData(this);
        lb7daysPic.setUserData(1);

        var lb14daysPicOn = new cc.MenuItemImage(gpNamespace.qqlb.Qqlb_14daysPic);
        var lb14daysPicOff = new cc.MenuItemImage(gpNamespace.qqlb.Qqlb_14daysPic);
        lb14daysPicOff.opacity = 128;
        var lb14daysPic = new cc.MenuItemToggle(lb14daysPicOff, lb14daysPicOn, this.lbCallback, this);
        this.lb14daysPic=lb14daysPic;
        lb14daysPic.setEnabled(false);
//        lb14daysPic.setUserData(this);
        lb14daysPic.setUserData(2);

        var lb21daysPicOn = new cc.MenuItemImage(gpNamespace.qqlb.Qqlb_21daysPic);
        var lb21daysPicOff = new cc.MenuItemImage(gpNamespace.qqlb.Qqlb_21daysPic);
        lb21daysPicOff.opacity = 128;
        var lb21daysPic = new cc.MenuItemToggle(lb21daysPicOff, lb21daysPicOn, this.lbCallback, this);
        this.lb21daysPic=lb21daysPic;
        lb21daysPic.setEnabled(false);
//        lb21daysPic.setUserData(this);
        lb21daysPic.setUserData(3);

        var lb28daysPicOn = new cc.MenuItemImage(gpNamespace.qqlb.Qqlb_28daysPic);
        var lb28daysPicOff = new cc.MenuItemImage(gpNamespace.qqlb.Qqlb_28daysPic);
        lb28daysPicOff.opacity = 128;
        var lb28daysPic = new cc.MenuItemToggle(lb28daysPicOff, lb28daysPicOn, this.lbCallback, this);
        this.lb28daysPic=lb28daysPic;
        lb28daysPic.setEnabled(false);
//        lb28daysPic.setUserData(this);
        lb28daysPic.setUserData(4);

        var lbMonthPicOn = new cc.MenuItemImage(gpNamespace.qqlb.Qqlb_MonthPic);
        var lbMonthPicOff = new cc.MenuItemImage(gpNamespace.qqlb.Qqlb_MonthPic);
        lbMonthPicOff.opacity = 128;
        var lbMonthPic = new cc.MenuItemToggle(lbMonthPicOff, lbMonthPicOn, this.lbCallback, this);
        this.lbMonthPic=lbMonthPic;
        lbMonthPic.setEnabled(false);
//        lbMonthPic.setUserData(this);
        lbMonthPic.setUserData(5);
        
//        var lbDailyPic = new cc.MenuItemImage(gpNamespace.qqlb.Qqlb_DailyPic, gpNamespace.qqlb.Qqlb_DailyPic, this.settingCallbackFunction, this);
//        var lb7daysPic = new cc.MenuItemImage(gpNamespace.qqlb.Qqlb_7daysPic, gpNamespace.qqlb.Qqlb_7daysPic, this.settingCallbackFunction, this);
//        var lb14daysPic = new cc.MenuItemImage(gpNamespace.qqlb.Qqlb_14daysPic, gpNamespace.qqlb.Qqlb_14daysPic, this.settingCallbackFunction, this);
//        var lb21daysPic = new cc.MenuItemImage(gpNamespace.qqlb.Qqlb_21daysPic, gpNamespace.qqlb.Qqlb_21daysPic, this.settingCallbackFunction, this);
//        var lb28daysPic = new cc.MenuItemImage(gpNamespace.qqlb.Qqlb_28daysPic, gpNamespace.qqlb.Qqlb_28daysPic, this.settingCallbackFunction, this);
//        var lbMonthPic = new cc.MenuItemImage(gpNamespace.qqlb.Qqlb_MonthPic, gpNamespace.qqlb.Qqlb_MonthPic, this.settingCallbackFunction, this);

        var lbListMenu = new cc.Menu(lbDailyPic, lb7daysPic, lb14daysPic,lb21daysPic,lb28daysPic,lbMonthPic);

        lbListMenu.attr({
            x: size.width/2,
            y: 208+lbDailyPic.height/2,
        });
        lbListMenu.alignItemsHorizontally();
        this.addChild(lbListMenu,2);

        gpNamespace.TyNetManager.getInstance().setSignLayer(this);
        xinyou.netManager.getSign();//获取签到
    },
    setSignInfo:function(){ 
    	console.log("累计登录天数："+signInfo.signCount);
    	//累计登录天数
    	var daysLayer = new tyNamespace.NumLayer(signInfo.signCount.toString(),19,13);
    	daysLayer.setPosition(339, 180);
    	this.addChild(daysLayer);
    	
    	//是否可签到 signInfo.canSign==0表示可签到
    	if(!userInfo.isSign){
    		cc.log("可签到");
    		this.lbDailyPic.setEnabled(true);
    		this.lbDailyPic.setSelectedIndex(1);
    	}
    	if(signInfo.signCount>=7){
    		if(signInfo.signs[0][2] == 1){
    			cc.log("7日礼包");
    			this.lb7daysPic.setEnabled(true);
    			this.lb7daysPic.setSelectedIndex(1);
    		}
    		if(signInfo.signCount>=14){
    			if(signInfo.signs[1][2] == 1){
    				cc.log("14日礼包");
    				this.lb14daysPic.setEnabled(true);
    				this.lb14daysPic.setSelectedIndex(1);
    			}
    			if(signInfo.signCount>=21){
    				if(signInfo.signs[2][2] == 1){
    				cc.log("21日礼包");
    				this.lb21daysPic.setEnabled(true);
    				this.lb21daysPic.setSelectedIndex(1);
    				}
    				if(signInfo.signCount>=28){
    					if(signInfo.signs[3][2] == 1){
    					cc.log("28日礼包");
    					this.lb28daysPic.setEnabled(true);
    					this.lb28daysPic.setSelectedIndex(1);
    					}
    					if(signInfo.signCount>=30){
    						if(signInfo.signs[4][2] == 1){
    						cc.log("30日礼包");
    						this.lbMonthPic.setEnabled(true);
    						this.lbMonthPic.setSelectedIndex(1);
    						}
    					}
    				}
    			}
    		}
    	}
    },
    
    lbCallback:function(sender){
    	var msgId = sender.getUserData();
    	if(msgId == 0){
//    		cc.log("日常签到："+msgId);
    		userInfo.isSign = true;
    		xinyou.netManager.setSign(1);
    	}else if(msgId >= 1){
//    		cc.log("连续签到："+msgId);
    		xinyou.netManager.duihuan(msgId);
    	}
    	sender.setEnabled(false);
    	sender.setSelectedIndex(0);
    },

    showRewards:function(isVip,isMore){
    	cc.log("奖励表现");
    	var gold = 1000;
    	if(isMore){
    		if(this.rewardIndex == 5){
    			gold = 5000;
    		}else{
    			gold = 2000;
    		}
    	}else{
    		if(isVip){
    			gold = 10000;
    		}
    	}
    	cc.log("奖励表现"+gold);
    	//金币图片
    	var goldPic = new cc.Sprite(gpNamespace.sctc.Sctc_Gold);
    	goldPic.attr({
    		x:cc.winSize.width/2 - 100,
    		y:cc.winSize.height/2
    	});
    	this.addChild(goldPic,10);
    	//乘号
    	var mul = new cc.Sprite("#sz_cdd_js_012u.png");
    	mul.attr({
    		x:90,
    		y:35
    	});
    	goldPic.addChild(mul);
    	//金额
    	var rewards = new tyNamespace.NumLayer(gold.toString(),18,0,"sz_cdd_js_");
    	rewards.attr({
    		x:140,
    		y:35
    	});
    	goldPic.addChild(rewards);

    	this.scheduleOnce(function(){
    		rewards.numFadeOut(1);
    		mul.runAction(new cc.FadeOut(1));
    		goldPic.runAction(new cc.FadeOut(1));
    	},1);
    }
    
});